<?php
defined('PT_ACCESS') or die('No direct access');

class Request extends BaseControllers{
	protected $registry;

	public function __construct($registry){
		$this->registry = $registry;
	}

	public function getVar($name, $default = null, $hash = 'default', $type = 'none', $mask = 0){
		// Ensure hash and type are uppercase
		$hash = strtoupper($hash);
		if ($hash === 'METHOD'){
			$hash = strtoupper($_SERVER['REQUEST_METHOD']);
		}
		$type = strtoupper($type);
		$sig = $hash . $type . $mask;

		// Get the input hash
		switch ($hash){
			case 'GET':
				$input = &$_GET;
			break;
			case 'POST':
				$input = &$_POST;
			break;
			case 'FILES':
				$input = &$_FILES;
			break;
			case 'COOKIE':
				$input = &$_COOKIE;
			break;
			case 'ENV':
				$input = &$_ENV;
			break;
			case 'SERVER':
				$input = &$_SERVER;
			break;
			default:
				$input = &$_REQUEST;
				$hash = 'REQUEST';
			break;
		}

		if (isset($GLOBALS['PT_REQUEST'][$name]['SET.' . $hash]) && ($GLOBALS['PT_REQUEST'][$name]['SET.' . $hash] === true)){
			// Get the variable from the input hash
			$var = (isset($input[$name]) && $input[$name] !== null) ? $input[$name] : $default;
			$var = $this->FilterInput->_cleanVar($var, $mask, $type);
		}elseif (!isset($GLOBALS['PT_REQUEST'][$name][$sig])){
			if (isset($input[$name]) && $input[$name] !== null){
				// Get the variable from the input hash and clean it
				$var = $this->FilterInput->_cleanVar($input[$name], $mask, $type);

				// Handle magic quotes compatibility
				if (get_magic_quotes_gpc() && ($var != $default) && ($hash != 'FILES')){
					$var = $this->FilterInput->_stripSlashesRecursive($var);
				}

				$GLOBALS['PT_REQUEST'][$name][$sig] = $var;
			}elseif ($default !== null){
				// Clean the default value
				$var = $this->FilterInput->_cleanVar($default, $mask, $type);
			}else{
				$var = $default;
			}
		}else{
			$var = $GLOBALS['PT_REQUEST'][$name][$sig];
		}

		return $var;
	}

}